import cv2 as cv
import numpy as np
import os
import random
def print_unique_value(img):
    unique_value=set()
    for row in img:
        for value in row:
            if(value in unique_value):
                pass
            else :
                print(value)
                unique_value.add(value)
def get_boxed_img(img,blocks):
    if len(img.shape)==2:
        out = cv.cvtColor(img, cv.COLOR_GRAY2BGR)
    else :
        out=img.copy()
    for block in blocks:
        p1=block.p1
        p2=block.p2
        cv.rectangle(out,(p1.x,p1.y),(p2.x,p2.y),(0,0,255),1)
    return out
def get_crack_boxed_img(img,cracks):
    if len(img.shape)==2:
        out = cv.cvtColor(img, cv.COLOR_GRAY2BGR)
    else :
        out=img.copy()
    for crack in cracks:
        p1=crack.bound.p1
        p2=crack.bound.p2
        cv.rectangle(out,(p1.x,p1.y),(p2.x,p2.y),(0,255,0),1)
    return out
def get_cluster_boxed_img(img,clusters):
    if len(img.shape)==2:
        out = cv.cvtColor(img, cv.COLOR_GRAY2BGR)
    else :
        out=img.copy()
    for cluster in clusters:
        p1=cluster.rect().p1
        p2=cluster.rect().p2
        cv.rectangle(out,(p1.x,p1.y),(p2.x,p2.y),(0,255,255),1)
    return out
def get_backbone_img(img,backbones,crackmanager):
    if len(img.shape)==2:
        out = cv.cvtColor(img, cv.COLOR_GRAY2BGR)
    else :
        out=img.copy()
    total_blocks_num=crackmanager.max_bid+1
    visit=[True for i in range(total_blocks_num)]
    for backbone in backbones:
        for block in backbone.blocks:
            block=crackmanager.blocks[block.bid]
            if visit[block.bid]==True:
                center1=block.get_rect().center
                # cv.circle(out,(center1.x,center1.y),2,(0,0,255),1)
                visit[block.bid]=False
                for adj in block.get_adj():
                    adj_block=crackmanager.blocks[adj]
                    center2=adj_block.get_rect().center
                    if visit[adj_block.bid]==True:
                        # cv.circle(out,(center2.x,center2.y),3,(0,0,255),1)
                        cv.line(out,(center1.x,center1.y),(center2.x,center2.y),(0,255,0),2)
                        # visit[adj_block.bid]=False
    return out


def generate_huge_img(shape,path,out_filename):
    H, W = shape
    #创建目标图片
    target_img = np.full((H, W), 0, dtype=np.uint8) 
    #获取要放入图片的文件夹路径
    #获取文件夹下所有图片文件名
    files = os.listdir(path)
    for file in files:
        #读取一张图片
        img = cv.imread(os.path.join(path, file), 0)
        img[img<=100]=0
        img[img>100]=1
        #随机生成放置位置
        h = random.randint(0, H-img.shape[0])
        w = random.randint(0, W-img.shape[1])
        #将图片写入到目标图片中
        target_img[h:h+img.shape[0], w:w+img.shape[1]] |= img  
    target_img[target_img==1]=255 
    #保存目标图片   
    cv.imwrite('output/'+out_filename, target_img)
def show_img(img,rect,name):
    out=img[int(rect.p1.y):int(rect.p2.y):1,int(rect.p1.x):int(rect.p2.x):1]
    out=np.reshape(out,(rect.height,rect.width))
    out*=255
    out=cv.resize(out,(rect.width*10,rect.height*10))
    cv.imshow(name,out)
    cv.waitKey(0)
